/*
 * WantToBuyDetailOfferListItem.java
 * business
 *
 * Created by chentao on 2017/5/12.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.listview.displaylistitem;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;

import com.yidiandao.business.R;
import com.yidiandao.business.data.OfferEntity;
import com.yidiandao.business.data.event.RefreshDiscoveryWantToBuyDetailEvent;
import com.yidiandao.business.data.model.IWantToBuyModel;
import com.yidiandao.business.data.model.WantToBuyModel;
import com.yidiandao.business.ui.widget.dialog.BusinessDialog;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.viewholder.WantToBuyDetailOfferHolder;
import com.yidiandao.business.utils.UIUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * 发现-求购详情-报价信息
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017/5/12 0012 16:06
 */
public class WantToBuyDetailOfferListItem extends DisplayListItem<WantToBuyDetailOfferHolder, OfferEntity> {

    private Context mContext;

    private WantToBuyModel mWantToBuyModel;

    public WantToBuyDetailOfferListItem(OfferEntity s) {
        super(s);
        mWantToBuyModel = new WantToBuyModel();
    }

    @Override
    public void onShow(Context context, WantToBuyDetailOfferHolder holder) {
        mContext = context;

        holder.refreshUI(data);

        holder.tvRepealOffer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCancelHintDialog();
            }
        });
    }

    private void showCancelHintDialog() {
        new BusinessDialog.Builder(mContext)
                .setTitle(R.string.cancel_offer)
                .setMessage(R.string.cancel_offer_message)
                .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        cancelOffer();
                    }
                })
                .setNegativeButton(R.string.cancel, null)
                .create()
                .show();
    }

    private void cancelOffer() {

        // 撤销报价
        mWantToBuyModel.cancelOffer(getDisplayItemTag(), data.getOfferId(), new IWantToBuyModel.OnNetListener<Object>() {
            @Override
            public void onSuccess(Object o) {
                // 不能取消
                UIUtils.showToast(UIUtils.getString(R.string.cancel_offer_success));
                EventBus.getDefault().post(new RefreshDiscoveryWantToBuyDetailEvent());
            }

            @Override
            public void onFailed(String message) {

            }
        });
    }

    @Override
    public String getDisplayItemTag() {
        return WantToBuyDetailOfferListItem.class.getSimpleName();
    }

    @Override
    public Class<WantToBuyDetailOfferHolder> getViewHolderClazz() {
        return WantToBuyDetailOfferHolder.class;
    }
}
